package com.nus.iss.ad.utils;

import java.util.HashMap;
import java.util.List;

import com.example.ad_project.R;
import com.nus.iss.ad.model.DeptRequest;
import com.nus.iss.ad.model.DeptRequestDetails;
 
import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
 
public class ExpandableListAdapter extends BaseExpandableListAdapter {
 
    private Context _context;
    private List<DeptRequest> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<DeptRequestDetails>> _listDataChild;
 
    public List<DeptRequest> get_listDataHeader() {
		return _listDataHeader;
	}

	public void set_listDataHeader(List<DeptRequest> _listDataHeader) {
		this._listDataHeader = _listDataHeader;
	}

	public HashMap<String, List<DeptRequestDetails>> get_listDataChild() {
		return _listDataChild;
	}

	public void set_listDataChild(
			HashMap<String, List<DeptRequestDetails>> _listDataChild) {
		this._listDataChild = _listDataChild;
	}

	public ExpandableListAdapter(Context context, List<DeptRequest> listDataHeader,
            HashMap<String, List<DeptRequestDetails>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
 
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition).getDepartmentName())
                .get(childPosititon);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
 
    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
 
        //final String childText = (String) getChild(groupPosition, childPosition);
    	final DeptRequestDetails childDetails = (DeptRequestDetails) getChild(groupPosition, childPosition);
 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }
 
        //lblListItem
        TextView txtiItemName = (TextView) convertView
                .findViewById(R.id.lblListItem);
        //qtyTV
        final TextView txtqty = (TextView) convertView
                .findViewById(R.id.qtyTV);
        
        final TextView errorText = (TextView) convertView
                .findViewById(R.id.errorTV);
        
        
        //qtyDeliveredTV
        final EditText txtqtyDelivered = (EditText) convertView
                .findViewById(R.id.qtyDeliveredET);
        
        txtiItemName.setText(childDetails.getItemName());
        txtqty.setText(Integer.toString(childDetails.getQty()));
        if(childDetails.getQtyDelivered() == 0){
        childDetails.setQtyDelivered(childDetails.getQty());
        }
        txtqtyDelivered.setText(Integer.toString(childDetails.getQtyDelivered()));
        
        
        // logic for the edit text...
        
        //final String val = ;
        
        txtqtyDelivered.addTextChangedListener(new TextWatcher() {
			 //String value = val; 
            public void afterTextChanged(Editable s) {  } 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {  } 
            
            public boolean isValidNumber(Class c, String numString) {
           	  try {
           	    if (c == double.class || c == Double.class) {
           	      Double.parseDouble(numString);
           	    } else if (c == int.class || c == Integer.class) {
           	      Integer.parseInt(numString);
           	    } else if (c == float.class || c == Float.class) {
           	      Float.parseFloat(numString);
           	    } else if (c == long.class || c == Long.class) {
           	      Long.parseLong(numString);
           	    }
           	  } catch (Exception ex) {
           	    return false;
           	  }
           	  return true;
           	}
            
            
            public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TEST", s+"  In onTextChanged");
            Log.d("TEST", txtqtyDelivered.getText().toString()+"  In onTextChanged");
            
            //if(requiredQty.getText() != null && requiredQty.getText().toString() != ""){
            if(s !="" && isValidNumber(Integer.class, s.toString())){
            int retrived = Integer.parseInt(txtqty.getText().toString());
            
            int expected = Integer.parseInt(s.toString());
            if(retrived < expected){
           	 errorText.setText("please enter proper value");
           	 //didm.setCheckFlag(true);
            }else {
           	 errorText.setText("");
			}
            
           }else {
           	 errorText.setText("please enter proper value");
			}
            
           if(errorText.getText().toString().equalsIgnoreCase("please enter proper value")){
        	   childDetails.setCheckFlag(true);
   		}else if(errorText.getText().toString().equalsIgnoreCase("")) {
   			childDetails.setQtyDelivered(Integer.parseInt(s.toString()));
   			childDetails.setCheckFlag(false);
   		}
           
           
            
            }
        }); 
        
        
		 
        
        
        
        return convertView;
    }
 
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition).getDepartmentName())
                .size();
    }
 
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
 
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
 
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
 
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        DeptRequest dep = (DeptRequest) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
        
        //representativeTV
 
        TextView lblRep = (TextView) convertView
                .findViewById(R.id.representativeTV);
        
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(dep.getDepartmentName());
        lblRep.setText(dep.getContactPerson());
        return convertView;
    }
 
    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}